package main.social.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import main.social.dao.EntryDao;
import main.social.dao.EntryGroupDao;
import main.social.dao.UserDao;
import main.social.entities.Entrys;
import main.social.entities.Users;
import main.social.service.EntryService;
import main.social.service.util.Utils;

public class EntryServiceImpl implements EntryService {

	EntryDao entryDao;
	EntryGroupDao entryGroupDao;
	UserDao userDao;
	
	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}

	public void setEntryGroupDao(EntryGroupDao entryGroupDao) {
		this.entryGroupDao = entryGroupDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> getWeeklyEntrys() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List weeklyMostView = this.getWeeklyMostView();
		result.put("weeklyMostView", weeklyMostView);
		
		List funny = this.getWeeklyMostViewByCatalog("FUNNY");
		result.put("funny", funny);
		
		List traffic = this.getWeeklyMostViewByCatalog("TRACFFIC");
		result.put("traffic", traffic);
		
		List family = this.getWeeklyMostViewByCatalog("FAMILY");
		result.put("family", family);
		
		List nature = this.getWeeklyMostViewByCatalog("NATURAL");
		result.put("nature", nature);
		
		List newest = this.getNewestEntrys();
		result.put("newest", newest);
		
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getWeeklyMostView() {
		return this.entryDao.getWeeklyMostView(7);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getWeeklyMostViewByCatalog(String catalog) {
		return this.entryDao.getMostViewByCatalog(catalog, 5);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getNewestEntrys() {
		return this.entryDao.getNewestEntrys(10);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getNewestEntrysByUser(String username) {
		Users user = this.userDao.getByUsername(username);
		return this.entryDao.getNewestEntrysByUser(user, 10);
	}

	@Override
	public Map<String, Object> getUserEntrysGroupByCatalog(String username) {
		Users user= this.userDao.getByUsername(username);
		return this.entryDao.getUserEntrysGroupByCatalog(user);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getUserEntrysByCatalog(String username, String catalog) {
		Users user = this.userDao.getByUsername(username);
		return this.entryDao.getUserEntrysByCatalog(user, catalog);
	}

	@Override
	public Entrys insertEntry(String username) {
		Users user = this.userDao.getByUsername(username);
		Entrys entry = this.entryDao.newEntityInstance();
		entry.setEntryGroup(this.entryGroupDao.getDefaultGroup());
		entry.setUser(user);
		entry.setUuid(Utils.createUUIDCode(20));
		this.entryDao.save(entry);
		return entry;
	}

	@Override
	public Entrys getEntryByUuid(String uuid) {
		return this.entryDao.getEntryByUuid(uuid);
	}

	@Override
	public boolean postComment(String username, String itemUuid, String comment) {
		Users user = this.userDao.getByUsername(username);
		if(user == null){
			return false;
		}
		Entrys entry = this.entryDao.getEntryByUuid(itemUuid);
		if(entry == null){
			return false;
		}
		Entrys childEntry = this.entryDao.newEntityInstance();
		childEntry.setEntryGroup(entry.getEntryGroup());
		childEntry.setUser(user);
		childEntry.setParent(entry);
		childEntry.setContent(comment);
		this.entryDao.save(childEntry);
		return true;
	}

	@Override
	public List<Entrys> getCommentEntrysByUuid(String uuid) {
		return this.entryDao.getEntrysChildByUuid(uuid);
	}

	@Override
	public Entrys increaseView(String uuid) {
		return this.entryDao.increaseView(uuid);
	}
	
	

}
